#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
long long T,l,r,dp[35][2][2]; 
long long s(long long n,long long kk)
{
	memset(dp,0,sizeof(dp));
	dp[30][0][0]=1;
	for(long long i=29;i>=0;i--)
	{
		for(long long j=0;j<=1;j++)
		{
			for(long long k=0;k<=1;k++)
			{
				for(long long ll=0;ll<=1;ll++)			
				{
					for(long long rr=0;rr<=ll;rr++)
					{
						if(!j&&ll&&!((n>>i)&1))
						{
							continue;
						}
						if(!k&&rr&&!((kk>>i)&1))
						{
							continue;
						}
						long long t1=((n>>i)&(ll^1))|j,t2=((kk>>i)&(rr^1))|k;
						dp[i][t1][t2]+=dp[i+1][j][k];
					}
				}
			}
		}
	}
	return dp[0][0][0]+dp[0][0][1]+dp[0][1][0]+dp[0][1][1];
}
int main()
{
	freopen("comb.in","r",stdin);
	freopen("comb.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>T;
	while(T--)
	{
		cin>>l>>r;
		cout<<s(r,r)-s(l-1,r)-s(r,l-1)+s(l-1,l-1)<<endl;
	}
	return 0;
}
